<template>
	<view>
		<view>
			<view class="mp-search-box">
				<input class="ser-input" type="text" v-model="val" placeholder="请输入您要搜索的内容" @click="getFocusSearch" />
				<!-- <view class="mp-search-button">
					<image src="../../static/icon_search.png" style="width: 24px;height: 24px;"></image>
				</view> -->
			</view>
		</view>
		<view class="content">
			<scroll-view scroll-y scroll-with-animation="true" :scroll-into-view="viewId" class="left-aside">
				<view v-for="(item,index) in flist" :key="item.FItemID" :id="item.FItemID" class="f-item b-b"
					:class="{active: item.FItemID === currentId}" @click="tabtap(item)">
					{{item.FName}}
				</view>
			</scroll-view>
			<scroll-view scroll-with-animation scroll-y class="right-aside" @scrolltolower="asideScroll"
				:scroll-top="tabScrollTop">
				<view v-if="slist!=''">
					<!-- <view v-for="item in slist" :key="item.FItemID" class="s-list" :id="'main-'+item.FItemID">
					<text class="s-item">{{item.FName}}</text> -->
					<view v-if="SonFlag==1">
						<view class="t-list">
							<view @click="navToList(1, titem.FItemID)" class="t-item" v-for="titem in slist"
								:key="titem.FItemID">
								<image :src="titem.FIcon||`../../static/errorImage.jpg`"></image>
								<text>{{titem.FName}}</text>
							</view>
						</view>
					</view>
					<view v-else="SonFlag==0">
						<view class="t-list">
							<view @click="navToList(1, titem.id)" class="t-item0" v-for="titem in slist"
								:key="titem.id">
								<image :src="titem.img||`../../static/errorImage.jpg`"></image>
								<view class="t-item0-right">
									<text>{{titem.title}}</text>
									<view class="price-box">
										<text class="price">{{titem.price}}</text>
										<!-- <text>已售 {{titem.qty || 0}}</text> -->
									</view>
								</view>
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
					<!-- </view> -->
				</view>
				<view class="s-list" v-else>
					<text class="s-item">暂无商品</text>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			uniLoadMore,
		},
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				SonFlag: 1, //
				loadingType: 'more', //加载更多状态
				pageindex: 1, //
				id: 1, //
				viewId: '',
				val:''
			}
		},
		onLoad(options) {
			if (JSON.stringify(options) != '{}') {
				this.id = options.id;
				this.viewId = options.id;
			}
			this.$corpid = uni.getStorageSync('corpid');
			this.$Url = uni.getStorageSync('URL')
			this.loadData();
		},
		onShow() {

		},
		methods: {
			async loadData() {
				uni.showLoading({
					title: '加载中...'
				})
				let list = await this.$api.json('cateList');
				list.forEach(item => {
					if (!item.pid) {
						// this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
					} else if (!item.picture) {
						// this.slist.push(item); //没有图的是2级分类
					} else {
						// this.tlist.push(item); //3级分类
					}
				})

				this.$util.util('Product/GetClassList', {
					corpid: this.$corpid
				}, 'POST').then(res => {
					console.log(res, this.currentId);
					if (this.id == 1) {
						this.id = res[0].FItemID;
					}
					console.log(this.id)
					this.flist = res;
					for (var i in res) {
						let id = this.id;
						if (this.id == res[i].FItemID) {
							this.currentId = res[i].FItemID
							this.SonFlag = res[i].FSonFlag;
							// this.$refs.id.scrollIntoView(false);
							// document.querySelector(id).scrollIntoView();
						}
					}
					console.log(this.SonFlag);
					this.ads();

				}).catch(error => {
					console.log(error);
				});
			},
			//一级分类点击
			tabtap(item) {
				uni.showLoading({
					title: '加载中...'
				})
				if (!this.sizeCalcState) {
					// this.calcSize();
				}

				this.currentId = item.FItemID;
				this.pageindex = 1;
				this.SonFlag = item.FSonFlag
				// let index = this.slist.findIndex(sitem=>sitem.pid === item.FItemID);
				this.ads();
			},
			ads() {
				let url = '',
					data = {
						corpid: this.$corpid,
						classid: this.currentId,
					}
				if (this.SonFlag == 1) {
					url = 'Product/GetClassItem';
				} else if (this.SonFlag == 0) {
					url = 'Product/GetGoodClass';
					data.pageindex = 0;
				}
				//二级分类
				this.$util.util(url, data, 'POSt').then(res => {
					console.log(res);
					this.slist = res;
					uni.hideLoading();
				}).catch(error => {
					console.log(error);
				});
			},
			//右侧栏滚动
			async asideScroll(e) {
				//加载商品
				if (this.SonFlag == 1) {
					return;
				}
				if (this.loadingType === 'nomore') {
					return;
				}
				uni.showLoading({
					title: '加载中...'
				})
				let data = {
					corpid: this.$corpid,
					classid: this.currentId,
					pageindex: this.pageindex++,
				}
				let goodsList = await this.$util.util('Product/GetGoodClass', data, 'POST');
				uni.hideLoading();
				if (goodsList.length != 0) {
					this.slist = this.slist.concat(goodsList);
					//判断是否还有下一页，有是more  没有是nomore
					this.loadingType = this.slist.length >= goodsList[0].total ? 'nomore' : 'more';
				} else {
					this.loadingType = 'nomore';
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.slist.forEach(item => {
					let view = uni.createSelectorQuery().select("#main-" + item.FItemID);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid) {
				if (this.SonFlag == 1) {
					uni.navigateTo({
						url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
					})
				} else {
					let id = 0;
					console.log(tid)
					uni.navigateTo({
						url: `/pages/product/product?id=${id}&goodsid=${tid}`
					})
				}

			},
			getFocusSearch() {
				uni.navigateTo({
				    url: '../search/search'
				});
			}
		}
	}
</script>

<style lang='scss'>
	.mp-search-box {
		position: relative;
		padding: 10upx 60upx;
		background-color: #f9f9d5;

		.ser-input {
			padding: 0 10px;
			height: 56upx;
			line-height: 56upx;
			font-size: 28upx;
			border-radius: 20px;
			background: rgba(255, 255, 255, .6);
		}

		.mp-search-button {
			position: absolute;
			top: 8px;
			right: 20px;
		}
	}

	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}

	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}

	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;

		&.active {
			color: $base-color;
			background: #f8f8f8;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
	}

	.s-item {
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: $font-color-dark;
	}

	.t-list {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		/* height: 1146rpx; */
		background: #fff;
		padding-top: 12upx;

		/* overflow: auto; */
		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;

		image {
			width: 140upx;
			height: 140upx;
		}
	}

	.t-item0 {
		width: 100%;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;

		image {
			width: 140upx;
			height: 140upx;
			float: left;
			/* border: 2rpx solid #000; */

		}

		.t-item0-right {
			width: 370rpx;
			height: 140rpx;
			float: left;
			margin-left: 10rpx;
			border-bottom: 4rpx solid #f8f8f8;
		}

		.price-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $font-color-light;
			margin-top: 60rpx;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 26upx;
			}
		}
	}
</style>
